from rest_framework.views import APIView
from rest_framework.permissions import IsAdminUser

from goods.models import GoodsVisitCount
from meiduo_admin.serializers.serializer import GoodsDayserializer
from users.models import User
from datetime import date, timedelta
from rest_framework.response import Response


# 获取用户总量
class UserTotalCountView(APIView):
    # 指定权限
    permission_classes = [IsAdminUser]


    def get(self, request):
        # 接收，
        # 验证
        # 处理
        # 查询用户总量


        count =User.objects.all().count()
        print("用户总量：",count)

        now_date=date.today()
        # 响应
        return Response({
            "date":now_date,
            "count":count
        })



# 日增用户
class UserDayCountView(APIView):
    # 1,指定权限
    permission_classes = [IsAdminUser]

    def get(self,requedt):
        # 处理
        day=date.today()
        # 查询当日用户数

        # 筛选条件注册日期（模型类父类属性）大于等于（gte）当天日期则为今日注册
        count = User.objects.filter(date_joined__gte=day).count()
        print("日增量：", count)
        return Response({
            "date":day,
            "count":count,

        })


# 日活跃用户
class UserActiveCountView(APIView):
    # 1,指定权限
    permission_classes = [IsAdminUser]

    def get(self,requedt):
        # 处理
        day=date.today()
        # 查询当日登录户数

        # 筛选条件注册日期（模型类父类属性）大于等于（gte）当天日期则为今日注册
        count = User.objects.filter(last_login__gte=day).count()
        print("日访问量：", count)
        return Response({
            "date":day,
            "count":count,

        })


# 日下单
class UserOrderCountView(APIView):
    # 1,指定权限
    permission_classes = [IsAdminUser]

    def get(self,requedt):
        # 处理
        day=date.today()
        # 查询当日下单户数

        # 筛选订单创建时间
        count = User.objects.filter(orders__create_time__gte=day).count()

        # 方法二，关联查询出对象，麻烦
        # users = User.objects.all()
        # for user in users:
        #     count=user.orders.filter(create_time__gte=day)
        #     print("日访问量：", count)
        #     return Response({
        #         "date":day,
        #         "count":count,
        #
        #     })

        return Response({
                "date":day,
                "count":count,

            })

# 月增用户

class UserMonthCountView(APIView):
    # 1,指定权限
    permission_classes = [IsAdminUser]

    def get(self,requedt):
        # 处理
        day=date.today()
        # 查询当月每日户数
        # 获取一个月前日期
        start_date = day - timedelta(29)
        # 创建空列表保存每天的用户量
        date_list = []

        for i in range(30):
            # 循环遍历获取当天日期
            index_date = start_date + timedelta(days=i)
            # 指定下一天日期
            cur_date = start_date + timedelta(days=i + 1)
            # 查询条件是大于当前日期index_date，小于明天日期的用户cur_date，得到当天用户量
            count = User.objects.filter(date_joined__gte=index_date, date_joined__lt=cur_date).count()

        return Response(
               date_list
            )


# 日分类商品访问量
class GoodsDayView(APIView):

    def get(self,request):
        # 获取当天日期
        now_date=date.today()
        # 获取当天访问的商品分类数量信息
        data=GoodsVisitCount.objects.filter(date=now_date)
        # 序列化返回分类数量
        ser=GoodsDayserializer(data,many=True)

        return Response(ser.data)


